#-*- coding:utf-8 -*-
import sys
import time
import datetime
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
from Widgets.calendarWindow import Calendar

class TimeInfo(QWidget):
    send_date_Signal=Signal(str)#将更改的日期给主窗口，然后在主窗口实现更替日期
    def __init__(self):
        super(TimeInfo,self).__init__()

        #全局变量
        self.given_time=''#当初始化主窗口时，传来的全局变量

        self.setMinimumWidth(250)
        self.mainLayout=QVBoxLayout()

        self.start_layout=QHBoxLayout()
        self.start_label=QLabel('开始时间:')


        self.date_btn=QPushButton('2019-07-12')
        self.date_btn.clicked.connect(self.__showCalender)
        self.date_btn.setFlat(True)
        self.date_btn.setStyleSheet("QPushButton:hover{color:rgb(255,165,0)}""QPushButton {Font-size :20px}")

        self.start_layout.addWidget(self.start_label)
        self.start_layout.addWidget(self.date_btn)

        self.left_layout=QHBoxLayout()
        self.left_label=QLabel('剩余时间:')
        self.countdown_label=QLabel('0')
        self.countdown_label.setFont(QFont('',55,75))
        self.day_label=QLabel('天')
        self.left_layout.addWidget(self.left_label)
        self.left_layout.addWidget(self.countdown_label)
        self.left_layout.addWidget(self.day_label)

        self.mainLayout.addLayout(self.start_layout)
        self.mainLayout.addLayout(self.left_layout)
        self.setLayout(self.mainLayout)


    def setLeftDay(self,start_day,given_time):
        self.given_time=given_time
        self.date_btn.setText(start_day)
        start=datetime.datetime.strptime(start_day,'%Y-%m-%d')
        today=datetime.datetime.strptime(time.strftime('%Y-%m-%d'),'%Y-%m-%d')
        used_day=(today-start).days

        left_day=int(given_time)-used_day
        # print(used_day,left_day)
        if left_day<2:
            self.countdown_label.setStyleSheet('color:rgb(255,0,0)')
        else:
            self.countdown_label.setStyleSheet('color:rgb(200,200,200)')

        self.countdown_label.setText(str(left_day))





    def __showCalender(self):
        self.cal=Calendar()
        self.cal.dateSignal.connect(self.__updateTimeInfo)
        self.cal.show()

    def __updateTimeInfo(self,date):
        print(date)
        self.date_btn.setText(date)
        self.setLeftDay(date,self.given_time)
        self.send_date_Signal.emit(date)

    def showTime(self):
        pass

if __name__ == '__main__':
    app=QApplication(sys.argv)
    c=TimeInfo()
    c.show()
    sys.exit(app.exec_())
